import { hbappRequest } from "/utils/requests";
const { infoArr } = require("../../utils/cityInfo");
const app = getApp();
Page({
  data: {
    navTop: app.globalData.navTop,
    statusBarHeight: app.globalData.statusBarHeight,
    titleBarHeight: app.globalData.titleBarHeight,
    area_name: "省级",
    indexData: {
      bkxmName: "事业单位",
    },
    bkxm: "",
    _num: 1,
    bkxmchange: false,
    tabScroll: 0,
    cateliTab: 0,
  },
  async onLoad(options) {
    xhs.showLoading({
      title: "数据加载中...",
    });
    let a = this;
    xhs.getSystemInfo({
      success: function (e) {
        app.globalData || (app.globalData = {}),
          -1 !== e.model.indexOf("iPhone")
            ? (app.globalData.titleBarHeight = 44)
            : (app.globalData.titleBarHeight = 48),
          (app.globalData.statusBarHeight = e.statusBarHeight),
          a.setData({
            statusBarHeight: app.globalData.statusBarHeight,
            titleBarHeight: app.globalData.titleBarHeight,
          });
      },
      failure: function () {
        a.setData({
          statusBarHeight: 0,
          titleBarHeight: 0,
        });
      },
    });
    let links,
      str = "";
    let jxOptions;
    if (options.scene) {
      options.scene = options.scene.replace(/%253D/g, ".");
      options.scene = options.scene.replace(/%2526/g, "-");
      jxOptions = await app.methods.getOPtions(options);
    }
    if (options.scene || options.id) {
      links = await app.methods.getLink(
        options.scene ? jxOptions.id : options.id
      );
    }
    options.area = links && links.area ? links.area : "hb";
    options.bkxm = links && links.bkxm ? links.bkxm : "";
    options.fx =
      !options || !options.scene || !options.id
        ? "hbwtb"
        : jxOptions.fx || options.fx;
    options.gh =
      !options || !options.scene || !options.id
        ? "zcj37368"
        : jxOptions.gh || options.gh;
    this.setData(await app.methods.getIdentity(options)); // 获取个人身份信息
    if (this.data.getIdentity.crm) {
      options.wxh = this.data.getIdentity.crm;
    } else {
      options.wxh = options.gh;
    }
    this.setData(await app.methods.getWechat(options)); // 获取个人身份信息
    this.setData(await app.methods.guishudi(options)); // 获取归属地
    let fenbu = this.data.fenbu;
    let fenxiao = this.data.fenxiao;
    let getIdentity = this.data.getIdentity;
    getIdentity.fenbu = fenbu;
    getIdentity.fenxiao = fenxiao;
    this.setData({
      options,
      getIdentity,
    });
    this.getArea(options);
    await this.getIncludeXm(this.data.area);
    await this.fixedXm();
    this.getDone();
    // console.log(this.data);
  },
  // 获取后缀中area信息
  getArea(options) {
    let area = options.area ? options.area : "hb";
    let area_name = "";
    options.area != undefined && options.area.length > 0
      ? area.indexOf("sjz") == 0
        ? (area_name = "石家庄")
        : area.indexOf("bd") == 0
        ? (area_name = "保定")
        : area.indexOf("ts") == 0
        ? (area_name = "唐山")
        : area.indexOf("hd") == 0
        ? (area_name = "邯郸")
        : area.indexOf("cd") == 0
        ? (area_name = "承德")
        : area.indexOf("cz") == 0
        ? (area_name = "沧州")
        : area.indexOf("qhd") == 0
        ? (area_name = "秦皇岛")
        : area.indexOf("zjk") == 0
        ? (area_name = "张家口")
        : area.indexOf("lf") == 0
        ? (area_name = "廊坊")
        : area.indexOf("hs") == 0
        ? (area_name = "衡水")
        : area.indexOf("xt") == 0
        ? (area_name = "邢台")
        : (area_name = "省站")
      : (area_name = "省站");
    this.setData({
      area,
      area_name,
    });
  },
  // 获取area中包含的项目
  async getIncludeXm(area) {
    return new Promise((resolve, reject) => {
      hbappRequest({
        url: "/hbqt/web/index.php/Contactus/xcxhd/index_xcxhomepage_list?callback=&",
        method: "GET",
        dataType: "jsonp",
        data: {
          area_code: area,
        },
      })
        .then((res) => {
          let zjz = app.methods.getAPIReturn(res);
          if (zjz.status == 1) {
            this.setData({
              xiangmu: zjz.data,
            });
            resolve(zjz.data);
          } else {
            this.setData({
              hasXm: false,
            });
            reject();
          }
        })
        .catch((err) => {
          reject();
        });
    });
  },
  // 首次取到项目并定位所在目录
  async fixedXm() {
    return new Promise(async (resolve, reject) => {
      if (this.data.options.bkxm) {
        this.setData({
          bkxm: this.data.options.bkxm,
        });
        await this.getIndex(this.data.options.bkxm, this.data.area);
        for (let i = 0; i < this.data.xiangmu.length; i++) {
          if (this.data.bkxm == this.data.xiangmu[i].cate_code) {
            this.setData({
              cateliTab: i,
            });
            // var t = xhs.getSystemInfoSync().windowWidth / 5;
            // this.setData({tabScroll: (i - 2) * t})
            // if (this.data.cateliTab == i) return !1;
          }
        }
        resolve();
      } else {
        await this.getIndex(this.data.xiangmu[0].cate_code, this.data.area);
        resolve();
      }
    });
  },
  // 登录首页
  async getIndex(bkxm, area) {
    return new Promise((resolve, reject) => {
      hbappRequest({
        url: "/hbqt/web/index.php/Contactus/xcxhd/index_xcx_homepage?callback=&",
        method: "GET",
        dataType: "jsonp",
        data: {
          cate_code: bkxm,
          area_code: area,
          lb3: 3
        },
      })
        .then(async (res) => {
          let zjz = app.methods.getAPIReturn(res);
          if (zjz.status == 1) {
            let { banner, gongju } = zjz.data;
            if (banner.lists && banner.lists.length > 0) {
              banner.lists = await this.removeLists(banner.lists);
            }
            if (gongju.lists && gongju.lists.length > 0) {
              gongju.lists = await this.removeLists(gongju.lists);
            }
            zjz.data.banner = banner;
            zjz.data.gongju = gongju;
            //操作成功
            this.setData({
              indexData: zjz.data,
              cate_code: bkxm,
              area_code: area,
              bkxm: bkxm,
            });
            resolve();
          } else if (zjz.info == "未找到该项目信息") {
            xhs.showModal({
              title: "出错啦",
              content: "未找到该项目信息，请尝试切换其它项目",
              confirmText: "确定",
            });
            resolve();
          }
        })
        .catch((err) => {
          console.log(err);
          resolve();
        });
    });
  },
  // 隐藏数据加载中并显示加载已完成
  getDone() {
    setTimeout(function () {
      xhs.hideLoading();
    }, 800);
    xhs.showLoading({
      title: "数据加载完成",
    });
    setTimeout(function () {
      xhs.hideLoading();
    }, 800);
  },
  // 切换项目
  async changeCate(options) {
    xhs.showLoading({
      title: "数据加载中...",
    });
    await this.getIndex(
      options.currentTarget.dataset.cate_code,
      options.currentTarget.dataset.area_code
    );
    this.fixedXm2(options.currentTarget.dataset.area_code, options);
    console.log(11)
    this.getDone();
  },
  // 内部切换项目是定位
  fixedXm2(area, options) {
    const e = options.currentTarget.dataset.cateli
      ? options.currentTarget.dataset.cateli
      : 0;
    const t = xhs.getSystemInfoSync().windowWidth / 5;
    this.setData({
      tabScroll: (e - 2) * t,
    });
    if (this.data.cateliTab == e) {
      this.setData({
        area_code: area,
        cateliTab: e,
        bkxmchange: true,
      });
    }
  },
  // 移除非xcx和html的内容
  async removeLists(arr) {
    let arr1 = [];
    return new Promise((resolve) => {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].type == "xcx" || arr[i].type == "html") {
          arr1.push(arr[i]);
        }
      }
      resolve(arr1);
    });
  },
  // 打开内容页
  openArticle(e) {
    let { type, src, title, url } = e.currentTarget.dataset;
    if (type == "html") {
      xhs.navigateTo({
        url: `/pages/zixun/zixun?src=${src}&title=${title}`,
      });
    } else if (type == "xcx") {
      String.prototype.replaceAll = function (s1, s2) {
        return this.replace(new RegExp(s1, "gm"), s2);
      };
      url = url.replaceAll("&amp;", "&");
      console.log(url);
      xhs.navigateTo({
        url: url,
      });
    }
  },
  // 打开咨询页面
  async zixun() {
    let { kf } = app.globalData.info;
    if (!kf) {
      let { kf, dyhewm, dyh, phone } = await this.getInfo();
      this.setData({
        kf,
        dyhewm,
        dyh,
        phone,
      });
      app.globalData.info = { kf, dyhewm, dyh, phone };
      app.methods.goZixun(kf);
    } else {
      app.methods.goZixun(kf);
    }
  },
  getInfo() {
    this.setData({
      authSetting: app.authSetting,
    });
    return new Promise(async (resolve) => {
      if (this.data.options.fx) {
        let info = await app.methods.getCity(infoArr, this.data.options.fx);
        console.log(info);
        resolve(info);
      } else {
        let info = await app.methods.noFenxiao(infoArr);
        resolve(info);
      }
    });
  },
});
